package com.application.Activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.application.Controller.ActivityController;
import com.application.Controller.ReadWriteFileController;


public class frmResultDanhGiaActivity extends Activity{
	public static ArrayList<String> arrayWork;
	public static ArrayAdapter<String> arrayAdapter;
	public static ListView list;
	public static ArrayList<ArrayList<String >> arraylist;
	
	//Lưu file XML
	String path = ReadWriteFileController.RATING_PATH;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_result_danh_gia);
		Log.d("PEERDROID","Da vao frmResultDanhGiaActivity");
		
		//Arraylist dung de add gia tri
		arrayWork = new ArrayList<String>(); 
		//Adapter dung de ket noi mang voi List View 
		arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayWork); 
		//ListView chua danh sach cong viec 
        list = (ListView) findViewById(R.id.listResult); 
        //Can set Adapter cho list de biet se lay noi dung tu mang arrayWork 
        list.setAdapter(arrayAdapter);
        
        final int count = ActivityController.get_countdg();
        Log.d("PEERDROID","So nguoi da danh gia: " + String.valueOf(count));
        
        //Arraylist chua noi dung danh gia lay tu ActivityController		
		arraylist = ActivityController.getArraylist();  
		
		if(arraylist == null){
			Log.d("PEERDROID","arraylist null");
		}
		Log.d("PEERDROID","arraylist get" + arraylist.get(0).get(0));
		for(int i=0; i<arraylist.size(); i++){
			float d = Float.parseFloat(arraylist.get(i).get(1));
			float kq = (d*20)/count;
			
			Log.d("PEERDROID","kq: " + String.valueOf(d));
			arrayWork.add(arraylist.get(i).get(0) + " : " + String.valueOf(kq)+"%");
			
		}
		arrayAdapter.notifyDataSetChanged();
		//ActivityController.getArraylist().clear();
		
		
		Button btnSave = (Button)findViewById(R.id.btnSave);
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ReadWriteFileController.writeRating(path, arraylist, count);
				showToast("Đã Lưu Xuống File");
			}
		});
	}
	
	private void showToast(String text) {
		Toast t = Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT);
		t.show();
	}
	
	
	/*@Override
	public void onResume() {
		Log.d("PEERDROID","Da vao onresume frmResultDanhGiaActivity");
		
		//Arraylist dung de add gia tri
		arrayWork = new ArrayList<String>(); 
		//Adapter dung de ket noi mang voi List View 
		arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayWork); 
		//ListView chua danh sach cong viec 
        list = (ListView) findViewById(R.id.listResult); 
        //Can set Adapter cho list de biet se lay noi dung tu mang arrayWork 
        list.setAdapter(arrayAdapter);
        
        int count = ActivityController.get_countdg();
        Log.d("PEERDROID","So nguoi da danh gia: " + String.valueOf(count));
        //Arraylist chua noi dung danh gia lay tu ActivityController		
		arraylist = ActivityController.getArraylist();  
		if(arraylist == null){
			Log.d("PEERDROID","arraylist null");
		}
		Log.d("PEERDROID","arraylist get" + arraylist.get(0).get(0));
		for(int i=0; i<arraylist.size(); i++){
			float d = Float.parseFloat(arraylist.get(i).get(1));
			float kq = (d*20)/count;
			
			Log.d("PEERDROID","kq: " + String.valueOf(d));
			arrayWork.add(arraylist.get(i).get(0) + " : " + String.valueOf(kq)+"%");
			
		}
		arrayAdapter.notifyDataSetChanged();
	}*/
	
	//**************************************
	//-------Ham con - SUB FUNCTION---------
	//**************************************
	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
		ActivityController.addActivityToArray(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}
	
}
